#!/bin/bash
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright (c) 2019, Joyent, Inc.
#

# All tests must pass!
set -o errexit

if [[ ! -f "/lib/sdc/.sdc-test-no-production-data" ]]; then
    cat <<EOF
To run this test you must create the file:

    /lib/sdc/.sdc-test-no-production-data

after ensuring you have no production data on this machine.
EOF
    exit 2
fi

start_time=$(date +%s)
test_log_dir="/tmp/vmtest.$start_time.$$"

if [[ $(uname -s) != 'SunOS' ]]; then
    echo "FATAL: this can only run on SmartOS"
    exit 1
fi

if [[ $(zonename) != 'global' ]]; then
    echo "FATAL: this can only run in the GZ"
    exit 1
fi

if [[ $(id -u) != 0 ]]; then
    echo "FATAL: this can only run as root"
    exit 1
fi

mkdir -p "$test_log_dir"

test_errors=0
failed_tests=

pwd=$(unset CDPATH; cd $(dirname $0); pwd)
for test in "$pwd/tests"/test-*.js; do
    test=${test##*/}
    set +o errexit
    set -o pipefail
    (
    export VMADM_DEBUG_LEVEL='debug'
    export VMADM_DEBUG_FILE="$test_log_dir/$test.log"
    /usr/vm/test/runtest "$pwd/tests/$test" 2>&1
    ) > >(tee -a /tmp/test.output.$$)
    TEST_EXIT_CODE=$?
    set +o pipefail
    set -o errexit
    if [[ ${TEST_EXIT_CODE} != 0 ]]; then
        test_errors=$((${test_errors} + 1))
        failed_tests=$(printf "${pwd}/tests/${test}\n${failed_tests}")
    fi
done

end_time=$(date +%s)
elapsed=$((${end_time} - ${start_time}))

tests=$(grep "^# tests [0-9]" /tmp/test.output.$$ | cut -d ' ' -f3 | xargs | tr ' ' '+' | bc)
passed=$(grep "^# pass  [0-9]" /tmp/test.output.$$ | tr -s ' ' | cut -d ' ' -f3 | xargs | tr ' ' '+' | bc)
skipped=$(grep "^# skip  [0-9]" /tmp/test.output.$$ | tr -s ' ' | cut -d ' ' -f3 | xargs | tr ' ' '+' | bc)
cores=$(grep "^# new core:" /tmp/test.output.$$ | awk '{print $NF}')

[[ -z ${tests} ]] && tests=0
[[ -z ${passed} ]] && passed=0
[[ -z ${skipped} ]] && skipped=0

fail=$((${tests} - ${passed} - ${skipped}))

if [[ -t 0 ]]; then
    PASS_COLOR="\033[32m"
    SKIP_COLOR="\033[31m"
    FAIL_COLOR="\033[31m"
    NORMAL_COLOR="\033[39m"
else
    PASS_COLOR=
    SKIP_COLOR=
    FAIL_COLOR=
    NORMAL_COLOR=
fi

echo "#"
echo "# $1 TEST COMPLETE IN ${elapsed} SECONDS, SUMMARY:"
echo "#"
echo -e "# ${PASS_COLOR}PASS: ${passed} / ${tests}${NORMAL_COLOR}"
if [[ ${skipped} -gt 0 ]]; then
    echo -e "# ${SKIP_COLOR}SKIP: ${skipped} / ${tests}${NORMAL_COLOR}"
fi
if [[ ${fail} -gt 0 ]]; then
    echo -e "# ${FAIL_COLOR}FAIL: ${fail} / ${tests}${NORMAL_COLOR}"
fi
if [[ -n ${failed_tests} ]]; then
    echo "#"
    echo "#  ** FAILED TESTS **"
    for test in ${failed_tests}; do
        echo -e "#  ${FAIL_COLOR}${test}${NORMAL_COLOR}"
    done
fi
if [[ -n "$cores" ]]; then
    echo "#"
    echo "# ** NEW CORE FILES **"
    for core in $cores; do
        echo -e "#  ${FAIL_COLOR}${core}${NORMAL_COLOR}"
    done
fi
echo "#"
echo "# log files available in: $test_log_dir"
echo "#"

if [[ ${fail} -gt 0 || ${test_errors} -gt 0 ]]; then
    exit 1
fi

exit 0
